﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace ThoiTrangT2B
{
    public partial class GioHang : System.Web.UI.Page
    {
        private static int cID;
        private static Dictionary<Int32, Int32> lstHang;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["GioHang"] != null)
            {
                if (!IsPostBack)
                {
                    lstHang = (Dictionary<Int32, Int32>)Session["GioHang"];
                    LoadSanPham(lstHang);
                }
            }
        }

        protected void LoadSanPham(Dictionary<Int32, Int32> lstID)
        {

            string lst = "";
            if (lstID.Count > 0)
            {
                foreach (var item in lstID)
                {
                    lst += item.Key.ToString() + ",";
                }
                lst = lst.Substring(0, lst.Length - 1);
            }
            else
            {
                lst = "0";
            }
            string sSql = "SELECT s.ID,s.Image,s.TenSP,(s.Gia - s.Gia*s.KM/100) AS Giacu FROM dbo.tblT2BSanPham s WHERE s.ID IN (" + lst + ")";
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sSql);
            grvGioHang.DataSource = dt;
            grvGioHang.DataBind();

        }

        protected void grvGioHang_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                Label lblGia = (Label)e.Row.FindControl("lblGia");
                Double CurPr = Convert.ToDouble(lblGia.Text);
                TextBox txtSL = (TextBox)e.Row.FindControl("txtSL");
                Label lblTongCong = (Label)e.Row.FindControl("lblTongCong");
                txtSL.Attributes.Add("onkeyup", "CalcSellPrice2(" + CurPr + ", '" + txtSL.ClientID + "','" + lblTongCong.ClientID + "')");
                txtSL.Text = lstHang.ElementAt(e.Row.RowIndex).Value.ToString();
                lblTongCong.Text = (CurPr * (Convert.ToInt32(txtSL.Text))).ToString("###,###");
            }
        }

        protected void grvGioHang_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int sID = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName == MyConst.Action.Delete)
            {
                foreach (var item in lstHang)
                {
                    if (item.Key == sID)
                    {
                        lstHang.Remove(item.Key);
                        break;
                    }
                }
                LoadSanPham(lstHang);
            }
        }

        protected void lbtThanhToan_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < grvGioHang.Rows.Count; i++)
            {
                TextBox sl = (TextBox)grvGioHang.Rows[i].FindControl("txtSL");
                int SoLuong = Convert.ToInt32(sl.Text);
                int key = lstHang.ElementAt(i).Key;
                lstHang[key] = SoLuong;
            }
            Session["GioHang"] = lstHang;
            Response.Redirect("ThanhToan.aspx");
        }

        protected void lblBack_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < grvGioHang.Rows.Count; i++)
            {
                TextBox sl = (TextBox)grvGioHang.Rows[i].FindControl("txtSL");
                int SoLuong = Convert.ToInt32(sl.Text);
                int key = lstHang.ElementAt(i).Key;
                lstHang[key] = SoLuong;
            }
            Session["GioHang"] = lstHang;
            Response.Redirect("./");
        }
    }
}
